package logic.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.ciko.code.bean.UserBean;

import java.util.ArrayList;
import java.util.List;

import logic.constants.DefaultConsts;
import logic.db.DBHelper;
import logic.table.UserTable;
import logic.table.UserTable.UserColumns;
import logic.util.logutil.LogUtil;


/**
 * 用户 数据操作类
 * 
 * @author 刘宾
 * @date 2014年8月11日 下午9:10:11
 * @editor zouqp 2014-10-11
 */
public class UserDao
{
	private static UserDao dao;
	private DBHelper       dbHelper;
	
	private UserDao(Context context) {
		dbHelper = DBHelper.getInstance(context);
	}
	
	public static UserDao getInstance(Context context) {
		dao = new UserDao(context);
		return dao;
	}
	
	/** 添加用户 */
	public boolean addUser(UserBean user) {
		if (user == null) return false;
		if (user.getLoginTime() == 0L)
		{
			user.setLoginTime(System.currentTimeMillis());
		}
		if (getUser(user.getAccount()) == null)
		{ //如果不存在就添加用户
			try
			{
				final ContentValues values = dbHelper.getContentValues(DBHelper.TYPE_INSERT, user);
				dbHelper.getSqliteDatabaseW().insert(UserTable.table, null, values);
				return true;
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			finally
			{
				dbHelper.closeSqliteDatabaseW();
			}
		}
		else
		{ //TODO ? 如果存在就更新用户（是否需要此操作待确认）
			return updateUser(user);
		}
		return false;
	}
	
	/** 获取用户 */
	public UserBean getUser(String account) {
		UserBean bean = null;
		final String selection = UserColumns.COLUMN_ACCOUNT + " = ?";
		final String[] args = new String[] {
			String.valueOf(account)
		};
		Cursor c = dbHelper.getSqliteDatabaseR().query(UserTable.table, null, selection, args, null, null, null);
		try
		{
			if (c != null && c.getCount() > 0)
			{
				c.moveToFirst();
				bean = dbHelper.getObject(UserBean.class, c);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
			dbHelper.closeSqliteDatabaseR();
		}
		return bean;
	}
	
	/** 获取所有用户 */
	public List<UserBean> getAllUser() {
		List<UserBean> list = null;
		UserBean bean = null;
		Cursor c = dbHelper.getSqliteDatabaseR().query(UserTable.table, null, null, null, null, null, null);
		try
		{
			if (c != null && c.getCount() > 0)
			{
				list = new ArrayList<UserBean>();
				while (c.moveToNext())
				{
					bean = dbHelper.getObject(UserBean.class, c);
					LogUtil.d(DefaultConsts.TAG, bean.toString());
					list.add(bean);
				}
				bean = null;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
			dbHelper.closeSqliteDatabaseR();
		}
		return list;
	}
	
	/** 更新用户 */
	public boolean updateUser(UserBean user) {
		if (user == null) return false;
		if (user.getLoginTime() == 0L)
		{
			user.setLoginTime(System.currentTimeMillis());
		}
		final String selection = UserColumns.COLUMN_ACCOUNT + " = ?";
		final String[] args = new String[] {
			String.valueOf(user.getAccount())
		};
		int v = 0;
		try
		{
			ContentValues values = dbHelper.getContentValues(DBHelper.TYPE_UPDATE, user);
			v = dbHelper.getSqliteDatabaseW().update(UserTable.table, values, selection, args);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeSqliteDatabaseW();
		}
		return v > 0 ? true : false;
	}
	
	/**
	 * 修改用户密码
	 * 
	 * @param user
	 * @return
	 */
	public boolean updateUserPwd(UserBean user) {
		final String selection = UserColumns.COLUMN_ACCOUNT + " = ?";
		final String[] args = new String[] {
			String.valueOf(user.getAccount())
		};
		int v = 0;
		try
		{
			ContentValues values = new ContentValues();
			values.put(UserColumns.COLUMN_PASSWORD, user.getPassword());
			v = dbHelper.getSqliteDatabaseW().update(UserTable.table, values, selection, args);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeSqliteDatabaseW();
		}
		return v > 0 ? true : false;
	}
	
	/** 获取最后一次登录的用户 */
	public UserBean getLastLoginUser() {
		final String selection = null;
		final String[] args = null;
		String orderBy = UserTable.UserColumns.COLUMN_LOGINTIME + " DESC";
		UserBean bean = null;
		Cursor c = dbHelper.getSqliteDatabaseR().query(UserTable.table, null, selection, args, null, null, orderBy);
		try
		{
			if (c != null && c.getCount() > 0)
			{
				c.moveToFirst();
				bean = dbHelper.getObject(UserBean.class, c);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			bean = null;
		}
		finally
		{
			dbHelper.closeCursor(c);
			dbHelper.closeSqliteDatabaseR();
		}
		return bean;
	}
	
	/** 删除用户 */
	public boolean deleteUser(String account) {
		final String selection = UserColumns.COLUMN_ACCOUNT + " = ?";
		final String[] args = new String[] {
			account
		};
		int v = dbHelper.getSqliteDatabaseW().delete(UserTable.table, selection, args);
		dbHelper.closeSqliteDatabaseW();
		return v > 0 ? true : false;
	}
	
	/**
	 * 
	 * @Title: updateNeteaseAccount 
	 * @Description: 云信账号登录后，更新云信账号的信息
	 * @param @param account
	 * @param @param im_account
	 * @param @param im_token
	 * @param @return    设定文件 
	 * @return int    返回类型 
	 * @throws
	 */
	public int updateNeteaseAccount(String account, String im_account, String im_token) {
		final String selection = UserColumns.COLUMN_ACCOUNT + " = ?";
		final String[] args = new String[] {
			account
		};
		int v = 0;
		try
		{
			ContentValues values = new ContentValues();
			values.put(UserColumns.COLUMN_NETEASE_ACCOUNT, im_account);
			values.put(UserColumns.COLUMN_NETEASE_TOKEN, im_token);
			v = dbHelper.getSqliteDatabaseW().update(UserTable.table, values, selection, args);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeSqliteDatabaseW();
		}
		return v;
	}
	
}
